package com.codetile.batch.db;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.batch.item.support.ClassifierCompositeItemWriter;
import org.springframework.batch.item.support.CompositeItemProcessor;
import org.springframework.batch.item.support.CompositeItemWriter;
import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.classify.Classifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.oxm.xstream.XStreamMarshaller;

import java.util.*;

@Configuration
public class MultiFileItemWriter {


//    @Bean
//    public CompositeItemWriter<User> multiFileItemWriter() throws Exception {
//        CompositeItemWriter<User> writer=new CompositeItemWriter<>();
//        writer.setDelegates(Arrays.asList(fileItem1Writer(),xml1Writer()));
//
//        writer.afterPropertiesSet();
//        return writer;
//    }

    /**
     * 实现分类
     * @return
     * @throws Exception
     */
    @Bean
    public ClassifierCompositeItemWriter<User> multiFile1ItemWriter(){
        ClassifierCompositeItemWriter<User> writer=new ClassifierCompositeItemWriter();
        writer.setClassifier(new Classifier<User, ItemWriter<? super User>>() {
            @Override
            public ItemWriter<? super User> classify(User user) {
                ItemWriter<User> write=null;
                try {
                    write=user.getId()%2==0?fileItem1Writer():xml1Writer();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return write;
            }
        });
        return writer;
    }


    @Bean
    public FlatFileItemWriter<User> fileItem1Writer() throws Exception {
        FlatFileItemWriter<User> writer=new FlatFileItemWriter<>();
        String path="F:\\user.txt";
        writer.setResource(new FileSystemResource(path));

        //把一个对象转换为一行字符串
        writer.setLineAggregator(new LineAggregator<User>() {
            ObjectMapper mapper=new ObjectMapper();
            @Override
            public String aggregate(User user) {
                String str=null;
                try {
                    str=mapper.writeValueAsString(user);
                }catch (JsonProcessingException e){
                    e.printStackTrace();
                }
                return str;
            }
        });
        writer.afterPropertiesSet();
        return writer;
    }

    @Bean
    public StaxEventItemWriter<User> xml1Writer() throws Exception {
        StaxEventItemWriter<User> writer=new StaxEventItemWriter<>();
        XStreamMarshaller marshaller=new XStreamMarshaller();
        Map<String, Class> map=new HashMap<>();
        map.put("user",User.class);
        marshaller.setAliases(map);
        writer.setRootTagName("users");
        writer.setMarshaller(marshaller);
        String path="f:\\user.xml";
        writer.setResource(new FileSystemResource(path));
        writer.afterPropertiesSet();
        return writer;
    }


}
